
import { Destroyable, react } from "xbsj-xe2/dist-node/xe2-base-utils";
import { ESPolygonFence } from 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main';
export class FenceCollection extends Destroyable {
    _map = new Map();

    // 全局显隐控制(EarthSDK响应式属性)
    _show = this.disposeVar(react(false));
    get show() { return this._show.value; }
    set show(value) { this._show.value = value }
    get showChanged() { return this._show.changed; }
    setData(data) {
        console.log("FenceCollection.setData", data);
        if (Array.isArray(data) && data.length > 0) {
            data.forEach(item => {
                if (!this._map.has(item.name)) this.create(item)
            })
        }
    }
    constructor(_myProjectManager) {
        super();
        this._myProjectManager = _myProjectManager
        this._sceneTree = _myProjectManager.createSceneTree("fenceSceneTree", 28)

        // 全局控制显隐属性，监听变化
        this.showChanged.don((val) => {
            this._map.forEach(item => {
                item.sceneObject.show = val
            })
        })
    }

    create(item) {
        const { id, points, name, height, mode } = item
        const treeItem = this._sceneTree.createSceneObjectTreeItem('ESPolygonFence', id)
        if (!treeItem) {
            console.error("围栏创建失败" + id)
            return
        }
        treeItem.sceneObject.points = points
        treeItem.sceneObject.height = height
        treeItem.sceneObject.name = name
        treeItem.sceneObject.show = this.show
        treeItem.sceneObject.materialMode = mode
        this._map.set(id, treeItem)
    }
}